//
//  TujiViewController.m
//  HomeIn
//
//  Created by dllo on 15/11/9.
//  Copyright © 2015年 ZZP. All rights reserved.
//

#import "TujiViewController.h"

@interface TujiViewController ()<UIScrollViewDelegate>

@property(nonatomic,strong)UIScrollView *mySCrollView;
@property(nonatomic,copy)NSString *setname;
@property(nonatomic,strong)UILabel *setnameLabel;
@property(nonatomic,strong)UILabel *imgtitleLabel;
@property(nonatomic,strong)UILabel *pageLabel;

@property(nonatomic,strong)NSMutableArray *pictureArray;
@property(nonatomic,strong)NSMutableArray *imgtitleArray;
@property(nonatomic,strong)NSMutableArray *imgurlArray;

@end

@implementation TujiViewController

//navigationBar颜色变黑
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.barTintColor =[UIColor blackColor];
}

//navigationBar恢复颜色
- (void)viewWillDisappear:(BOOL)animated {
    
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.barTintColor = RGBA(251, 106, 10, 1);
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.imgurlArray = [NSMutableArray array];
    self.imgtitleArray = [NSMutableArray array];
    self.view.backgroundColor = [UIColor blackColor];
    
    //导航栏左侧按钮
    UIImage *image = [UIImage imageNamed:@"iconfont-unie642.png"];
    //取消渲染
    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(leftBarAction)];
    
    self.setnameLabel = [[UILabel alloc]initWithFrame:CGRectMake(10*WIDTHSCALE, (64+30+400+50)*HEIGHTSCALE, 315*WIDTHSCALE, 30*HEIGHTSCALE)];
    self.setnameLabel.textColor = [UIColor whiteColor];
    self.setnameLabel.font = [UIFont boldSystemFontOfSize:15];
    [self.view addSubview:self.setnameLabel];
    
    self.imgtitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10*WIDTHSCALE, self.setnameLabel.frame.origin.y+self.setnameLabel.frame.size.height+1, 200*WIDTHSCALE, 30*HEIGHTSCALE)];
    [self.view addSubview:self.imgtitleLabel];
    self.imgtitleLabel.font = [UIFont systemFontOfSize:17];
    self.imgtitleLabel.textColor = [UIColor whiteColor];
    
    self.pageLabel = [[UILabel alloc]initWithFrame:CGRectMake(325*WIDTHSCALE, self.setnameLabel.frame.origin.y, 50*WIDTHSCALE, 30*HEIGHTSCALE)];
    [self.view addSubview:self.pageLabel];
    self.pageLabel.font = [UIFont systemFontOfSize:17];
    self.pageLabel.textColor = [UIColor whiteColor];
    
    NSString *str1,*str2;
    if (![self.photosetID isEqual:@""]) {
        str1 = [self.photosetID substringWithRange:NSMakeRange(4, 4)];
        str2 = [self.photosetID substringWithRange:NSMakeRange(9, 5)];
    }
    
    [self getAFNetWorkingData:str1 Str:str2];
    
}

- (void)getAFNetWorkingData:(NSString *)string Str:(NSString *)str
{
    
    NSString *urlString = [NSString stringWithFormat:@"http://c.m.163.com/photo/api/set/%@/%@.json", string, str];
    
    //[NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];  代表支持所有的接口类型
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [manager GET:urlString parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSMutableDictionary *Dic = responseObject;
        self.setname = [Dic objectForKey:@"setname"];
        self.setnameLabel.text = self.setname;
        NSMutableArray *array = [Dic objectForKey:@"photos"];
        for (NSMutableDictionary *dic in array) {
            NSString *imgurl = [dic objectForKey:@"imgurl"];
            NSString *imgtitle = [dic objectForKey:@"imgtitle"];
            [self.imgurlArray addObject:imgurl];
            [self.imgtitleArray addObject:imgtitle];
        }
        self.imgtitleLabel.text = [self.imgtitleArray objectAtIndex:0];
        self.pageLabel.text = [NSString stringWithFormat:@"1/%ld",self.imgtitleArray.count];
        
        [self setLunboPicture];//方法传值
        
    }
         failure:^(AFHTTPRequestOperation *operation, NSError *error) {
             
             NSLog(@"error = %@",error);
             
         }];
    
}

-(void)setLunboPicture
{
    self.mySCrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, (30+64)*HEIGHTSCALE, 375*WIDTHSCALE, 400*HEIGHTSCALE)];
    self.mySCrollView.contentSize = CGSizeMake(375*WIDTHSCALE * self.imgurlArray.count, 0);
    self.mySCrollView.pagingEnabled = YES;
    self.mySCrollView.backgroundColor = [UIColor blackColor];
    self.mySCrollView.contentOffset = CGPointMake(0, 0);
    self.mySCrollView.userInteractionEnabled = YES;
    self.mySCrollView.delegate = self;
    [self.view addSubview:self.mySCrollView];
    
    for (int index = 0; index < self.imgurlArray.count; index++)
    {
        UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(self.mySCrollView.frame.origin.x + self.mySCrollView.frame.size.width * index, 0, self.mySCrollView.frame.size.width, self.mySCrollView.frame.size.height)];
        NSString *imgurl = [self.imgurlArray objectAtIndex:index];
        [imageView sd_setImageWithURL:[NSURL URLWithString:imgurl] placeholderImage:[UIImage imageNamed:@"icon.png"]];
        imageView.userInteractionEnabled = YES;
        [self.mySCrollView addSubview:imageView];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self  action:@selector(tapAction:)];
        [imageView addGestureRecognizer:tap];
        
    }
}

-(void)tapAction:(UITapGestureRecognizer *)tap
{
    UIImageView  *imageView = (UIImageView *)tap.view;
    
    UIImageWriteToSavedPhotosAlbum(imageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
    
}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
    
    if (error == nil) {
        
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"已存入手机相册" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alert show];
        
    }else{
        
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"保存失败" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alert show];
    }
    
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    self.mySCrollView.contentSize = CGSizeMake(375*WIDTHSCALE * self.imgurlArray.count, 0);
    self.imgtitleLabel.text= [self.imgtitleArray objectAtIndex:self.mySCrollView.contentOffset.x/(375*WIDTHSCALE)];
    self.pageLabel.text=[NSString stringWithFormat:@"%d/%ld",(int)(self.mySCrollView.contentOffset.x/(375*WIDTHSCALE)+1),self.imgtitleArray.count];
}


- (void)leftBarAction
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
